// pages/general/perfectInformation/perfectInformation.js
var Api =require("../../../utils/util")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    radio: '1',
    i_code:'',   //手机验证码
    i_value:"",  //电话
    m_name:'',  //姓名
    i_city:'',    //所在城市
    i_dealer:'',//经常采购的经销商
    code_status:'发送验证码',
    time:60,
    whetheruse:true,
    isSendCode:false,
    option1: [
      { text: '电工', value: 0 },
      { text: '项目经理', value: 1 },
      { text: '其他', value: 2 },
    ],
    value1: 0,
    date: '',
  },
  
  changeDate(e){
    this.setData({ date:e.detail.value});
    },
  onChange(event) {
    this.setData({
      radio: event.detail,
    });
  },
  // 职业下拉列表
  onChangeOccupation(event) {
    this.setData({
      value1: event.detail
    })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  i_name:function(e){
    this.setData({
      i_value: e.detail.value , 
    })
  },
  m_name:function(e){
    this.setData({
      m_name: e.detail.value , 
    })
  },
  i_code:function(e){
    this.setData({
      i_code: e.detail.value , 
    })
  },
  i_dealer:function(e){
    this.setData({
      i_dealer: e.detail.value , 
    })
    console.log('i_dealer',this.data.i_dealer)
  },
  i_city:function(e){
    this.setData({
      i_city: e.detail.value , 
    })
    console.log(this.data.i_city)
  },
  verify:function(){
    if(this.data.m_name == '' || this.data.i_code == '' || this.data.i_value == '' || this.data.i_city == ''|| this.data.i_dealer == ''　){
      wx.showToast({
        title: '请填写完整信息',
        duration: 2000,
        icon:'none'
        });
    }else{
      this.submitinfo()
    }
  },
  submitinfo:function(){
    var that = this;
    for(var i = 0;i < that.data.option1.length;i++){
      if(that.data.option1[i].value == that.data.value1){
        that.setData({
          status:that.data.option1[i].text
        })
      }
    }
    console.log('status',that.data.status)
    wx.request({
      url: getApp().globalData.baseUrl + '/wago/user/addgerenxinxi',
      method:'post',
      data: {
        opneid:getApp().globalData.openid,
        code:that.data.i_code,
        phone:that.data.i_value,
        name:that.data.m_name,
        officialAccount:getApp().globalData.attention,
        city:that.data.i_city,
        cannel:that.data.radio,
        status:that.data.status,
        distributor:that.data.i_dealer
      },
      success: function (res) {
        that.setData({
          nowstatus:res.statusCode
        });
        wx.showToast({
          title: '提交成功',
          duration: 2000,
          icon: 'none'
        });
        wx.switchTab({
          url: '/pages/personal/personal',
          });
      }
    })
  },
   sendMessageCode: function () {
    var that = this;
    wx.request({
      url: getApp().globalData.baseUrl + '/wago/validateCode/sendforLogin',
      method:'get',
      data: {
        phone:that.data.i_value
      },
      success: function (res) {
        that.setData({
          nowstatus:res.statusCode
        })
      }
    })
  },
  checkCode:function(){
    var that = this;
    wx.request({
      url: getApp().globalData.baseUrl + '/wago/user/checkPhone',
      method:'post',
      data: {
        'phone':that.data.i_value
      },
      success: function (res) {
        
      }
    })
  },
  sendCode: function(){
    var that = this;
     let vm = this; 
    if (!(/^1[34578]\d{9}$/.test(this.data.i_value))) {

      wx.showToast({
      
      title: '手机号码有误',
      
      duration: 2000,
      
      icon:'none'
      
      });
      
      return false;
      
      }

      wx.request({
        url: getApp().globalData.baseUrl + '/wago/user/checkPhone',
        method:'post',
        data: {
          'phone':that.data.i_value
        },
        success: function (res) {

          console.log(res.data.flag)
          if(res.data.flag){
            that.sendMessageCode();
           
            vm.setData({
              isSendCode:true
            })
        vm.data.timer = setInterval(()=>{
          vm.setData({
            time:vm.data.time-1
          })
          if(vm.data.time===0){
            clearInterval(vm.data.timer);
            vm.setData({
              timer:null,
              isSendCode:false,
              time:60
            })
          }
        },1000)

          }else{
            wx.showToast({
      
              title: '手机号已存在',
              
              duration: 2000,
              
              icon:'none'
              
              });
          }
        }
      })


   
     


  },
  getadmincomment:function(url){

    var that=this;
    
    wx.request({
    
      url: url,
    
      data: {},
    
      method: 'post', 
    
      success: function(res){
    
        var datas = res.data.objects;
    
        //在绑定数据之前处理 评论的时间 然后再用setData绑定值
    
        for(var i=0; i<datas.length; i++){
    
           // 时间戳装换  
    
           //目的是转换格式
    
           //设置setData 中的 conmment_time
    
           that.conmmentcreateDate(datas[i].createDate);
    
           //通过that 拿到 conmment_time的值值for循环出来
    
           datas[i].createDate= that.data.conmment_time
    
          // console.log("ee"+ datas[i].createDate);
    
        }
    
       
    
       //然后 改好时间格式 遍历出来就好
    
        that.setData({
    
          admin_comment_data:datas,
    
          
    
        })
    
        
    
      },
    
      fail: function() {
    
        // fail
    
      },
    
      complete: function() {
    
        // complete
    
      }
    
    })
  }

})